
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author  liuchun
 *
 */public class MainFrame extends JFrame {
     //定义一个蛇
    private Snake snake = new Snake();
    //定时器，每隔一段时间重绘游戏棋盘
    private Timer timer;
    private JPanel jPanel = null;

    public MainFrame(){
        //初始化窗体参数
        initFrame();
        //初始化游戏面板
        initPanel();
        //初始化定时器
        initTimer();
        //键盘监听
        setkeyListener();
    }

    private void initTimer() {
        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                //蛇如果还活着，移动
                if (snake.isLive) {
                    snake.move();
                }else {
                    return;
                }
                //判断蛇是否死亡
                snake.checkOver();
                if (snake.isLive == false){
                    JOptionPane.showMessageDialog(null,"游戏结束");
                    return;
                }
                //每次移动调用方法，重绘蛇
                jPanel.repaint();
            }
            };
        timer.scheduleAtFixedRate(task,0,80);
        }

    //初始化游戏面板
    public void initPanel() {
            jPanel = new JPanel() {
                @Override
                public void paint(Graphics g) {
                    g.clearRect(0, 0, 600, 600);
                    //设置画笔颜色
                    g.setColor(Color.black);
                    //绘制40条横线
                    for (int i = 1; i <= 40; i++) {
                        g.drawLine(0, 15 * i, 600, 15 * i);
                        g.drawLine(15 * i, 0, 15 * i, 600);
                    }
                    //绘制蛇
                    for (Node node : snake.getBody()) {
                        //设置画笔颜色
                        g.setColor(Color.green);
                        g.fillRect(node.getX() * 15, node.getY() * 15, 15, 15);
                    }
                }
            };
            add(jPanel);
        }
    private void setkeyListener() {
        //对上下左右键进行监听
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()){
                    //按下上键
                    case KeyEvent.VK_UP:
                        //蛇头方向改为上
                        snake.setDirection(Direction.UP);
                        break;
                    //按下下键
                    case KeyEvent.VK_DOWN:
                        //蛇头方向改为下
                        snake.setDirection(Direction.DOWN);
                        break;
                    //按下左键
                    case KeyEvent.VK_LEFT:
                        //蛇头方向改为左
                        snake.setDirection(Direction.LEFT);
                        break;
                    //按下右键
                    case KeyEvent.VK_RIGHT:
                        //蛇头方向改为右
                        snake.setDirection(Direction.RIGHT);
                        break;
                }
            }
        });
    }


    //初始化窗体参数
    public void initFrame(){
        //设置标题
        setTitle("贪吃蛇");
        setSize(610,640);
        setLocation(600,250);
        //设置窗体大小不可变
        setResizable(false);
        //设置窗体关闭按钮是正常退出
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new MainFrame().setVisible(true);
    }
}
